package com.liulei.service.impl;

import cn.hutool.core.util.StrUtil;
import com.liulei.entities.Salary;
import com.liulei.entities.Worker;
import com.liulei.mapper.SalaryMapper;
import com.liulei.service.SalaryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SalaryServiceImpl implements SalaryService {
    /**
     * 自动装配SalaryMapper对象
     */
    @Autowired
    private SalaryMapper salaryMapper;

    /**
     * 计算所有送水工的工资
     * @return 工资列表
     */
    @Override
    public List<Salary> listCalcSalary() {
        return salaryMapper.listCalcSalary();
    }

    /**
     * 根据搜索条件计算某一段时间的工资
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 工资列表
     */
    @Override
    public List<Salary> calcSalaryByCondition(String startDate, String endDate) {
        //条件成立：表示“结束时间”为空，系统当前时间为结束时间。然后在转换为字符串
        if (StrUtil.isEmpty(endDate)) {
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            endDate = sdf.format(date);
        }
        // salaryList 在某个时间段已经为客户送过水的送水工信息
        List<Salary> salaryList = salaryMapper.calcSalaryByCondition(startDate,endDate);
        //查询没有为客户送过水的送水工信息
        List<Worker> workerList = salaryMapper.queryNotSendWaterWorker();
        //获取已送过水的送水工的送水工信息
        List<String> workerNameList = salaryList.stream().map(Salary::getWorkerName).collect(Collectors.toList());
        // 遍历每个没有为客户送过水的送水工信息，将它们注入到Salary对象。最后把Salary对象添加到salaryList集合
        workerList.forEach(worker -> {
            if (!workerNameList.contains(worker.getWorkerName())) {
                Salary salary = new Salary();
                salary.setWorkerName(worker.getWorkerName());
                salary.setWorkerSalary(worker.getWorkerSalary());
                salary.setWorkerMoney(worker.getWorkerMoney());
                //没有送过水的送水工实发工资就是他的底薪
                salary.setFinalSalary(Double.valueOf(worker.getWorkerSalary()));
                //没有送水的送水工送水数量为0
                salary.setSendWaterCount(0);
                salaryList.add(salary);
            }
        });
        // 将“实发工资”按照降序排序
        // 需要对每个送水工的”实发工资“进行比较
        Collections.sort(salaryList,(o1, o2)->{
            if(o1.getFinalSalary() > o2.getFinalSalary()){
                return -1;
            } else if (o1.getFinalSalary() < o2.getFinalSalary()){
                return 1;
            } else {
                return 0;
            }
        });
        Collections.sort(salaryList, new Comparator<Salary>() {
            @Override
            public int compare(Salary o1, Salary o2) {
                if (o1.getFinalSalary() > o2.getFinalSalary()) {
                    return -1;
                }else if (o1.getFinalSalary() < o2.getFinalSalary()) {
                    return 1;
                }else {
                    return 0;
                }
            }
        });
        //// salaryList集合对象可能有重复的数据，将重复数据去掉
//        Set set = new LinkedHashSet<>(salaryList);
//        salaryList.clear();
//        salaryList.addAll(set);
        return salaryList;
    }

}
